<?php
namespace App\Http\Controllers\Web\Member\User;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;
use Mews\Captcha\Captcha;

class UserController extends BaseController
{
    protected  $api_url = [
        'show' => ['get', 'member/member/%d'],
        'update' => ['put', 'member/member/%d'],
        'verifycode' => ['get', 'verifycode'],
        'sendemail' => ['get', 'sendemail'],
        'verifyemailcode' => ['get', 'verifyemailcode'],
        'shop'=>['get','member/member/shop'],
        'shop_auth'=>['get','member/seller/shop_auth']
    ];


    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    //基本信息视图
    public function infoAction(){
        //用户信息查询
        //var_dump($this->request->session()->all());
        $ret = http_web($this->api_url['show'],[session('user_id')])->send();
        $data = [
          'data'=>  $ret['data']['data'],
        ];
        return view('web.user.member.info')->with($data);
    }

    //修改基本信息
    public function updateInfoAction(){

        $params = [
            'pic' => $this->request->input('path'),
            'sex' => $this->request->input('sex'),
            'uptype' => 'upbasic',
        ];
        $data = http_web($this->api_url['update'],[session('user_id')])->send($params);
        session('user_info')['pic']=$this->request->input('path');
        return $data;
    }


    //账户安全视图
    public function safeAction(){
        //用户信息查询
        $ret = http_web($this->api_url['show'],[session('user_id')])->send();
        $data = [
            'data'=>  $ret['data']['data'],
        ];
        //var_dump(session('user_shop_id'));
        return view('web.user.member.safe')->with($data);
    }



    //登陆密码修改视图
    public function pwdReviseAction(){
        $data=[
            'phone'=>session('user_phone'),
            'message' => $this->request->input('message')
        ];
        return view('web.user.member.pw_revise')->with($data);
    }

    public function pwdReviseTwAction(){
        $this->validate($this->request, [
            'code' => 'required',
            'captcha' => 'required|captcha',
        ],[
            'captcha' => '图形验证码错误'
        ]);
        $all = $this->request->all('code');

        $phone = session('user_phone');
        $code = $all['code'] ? $all['code'] : '';

        $param = ['phone' => $phone,'code' => $code];
        $data = http_web($this->api_url['verifycode'])->send($param);

        if($data['code'] == 0){
            return view('web.user.member.pw_revise2')->with(['data'=>$param]);
        }else{
            return redirect('/member/user/pwd_revise?message='.$data['message']);
        }

    }

    public function pwdReviseThAction(){
        $this->validate($this->request, [
            'pwd' => 'required',
        ],[
            'pwd' => '密码不能为空'
        ]);

        $params = [
            'pwd' => $this->request->input('pwd'),
            'uptype' => 'uppwd'
        ];
        $data = http_web($this->api_url['update'],[session('user_id')])->send($params);

        if($data['code'] == 0){
            return view('web.user.member.pw_revise3');
        }else{
            return redirect('/member/user/pw_revise');
        }

    }

    //手机修改视图
    public function phoneReviseAction(){
        $data=[
            'phone'=>session('user_phone'),
            'message' => $this->request->input('message')
        ];
        return view('web.user.member.ph_revise')->with($data);
    }

    public function phoneReviseTwAction(){
        $this->validate($this->request, [
            'code' => 'required',
            'captcha' => 'required|captcha',
        ],[
            'captcha' => '图形验证码错误'
        ]);
        $all = $this->request->all('code');

        $phone = $this->session('user_phone');
        $code = $all['code'] ? $all['code'] : '';

        $param = ['phone' => $phone,'code' => $code];
        $data = http_web($this->api_url['verifycode'])->send($param);

        if($data['code'] == 0){
            return view('web.user.member.ph_revise2')->with(['data'=>$param]);
        }else{
            return redirect('/member/user/phone_revise?message='.$data['message']);
        }

    }

    public function phoneReviseThAction(){
        if($this->request->input('type') == 'update'){
            $params = [
                'phone' => $this->request->input('phone'),
                'code' => $this->request->input('code')
            ];
            $data = http_web($this->api_url['verifycode'])->send($params);

            if($data['code'] == 0){
                $params_a = [
                    'phone' => $this->request->input('phone'),
                    'oldphone' => session('user_phone'),
                    'uptype' => 'upphone'
                ];
                $data_a = http_web($this->api_url['update'],[session('user_id')])->send($params_a);

                return $data_a;

            }else{
                return $data;
            }

        }else{
            return view('web.user.member.ph_revise3');
        }

    }



    //修改邮箱视图
    public function emailReviseAction(){
        $data=[
            'email'=>session('user_email') ? session('user_email') : '还未绑定邮箱',
            'message' => $this->request->input('message')
        ];
        return view('web.user.member.email_revise')->with($data);
    }

    public function emailReviseTwAction(){
        $this->validate($this->request, [
            'email' => 'required|email',
            'captcha' => 'required|captcha',
        ],[
            'captcha' => '图形验证码错误'
        ]);
        $all = $this->request->all('email');

        $email = $all['email'] ? $all['email'] : '';

        $param = ['email' => $email];
        $data = http_web($this->api_url['sendemail'])->send($param);

        if($data['code'] == 0){
            return view('web.user.member.email_revise2')->with(['data'=>$param]);
        }else{
            return redirect('/member/user/email_revise?message='.$data['message']);
        }

    }

    public function emailReviseThAction(){

        if($this->request->input('type') == 'update'){
            $params = [
                'email' => $this->request->input('email'),
                'code' => $this->request->input('code')
            ];
            $data = http_web($this->api_url['verifyemailcode'])->send($params);

            if($data['code'] == 0){
                $params_a = [
                    'email' => $this->request->input('email'),
                    'uptype' => 'upemail'
                ];
                $data_a = http_web($this->api_url['update'],[session('user_id')])->send($params_a);

                return $data_a;

            }else{
                return $data;
            }

        }else{
            return view('web.user.member.email_revise3');
        }


    }


    //支付密码修改视图
    public function payReviseAction(){
        $data=[
            'phone'=>session('user_phone'),
            'message' => $this->request->input('message')
        ];
        return view('web.user.member.pay_revise')->with($data);
    }

    public function payReviseTwAction(){

        $this->validate($this->request, [
            'code' => 'required',
            'captcha' => 'required|captcha',
        ],[
            'captcha' => '图形验证码错误'
        ]);
        $all = $this->request->all('code');

        $phone = session('user_phone');
        $code = $all['code'] ? $all['code'] : '';

        $param = ['phone' => $phone,'code' => $code];
        $data = http_web($this->api_url['verifycode'])->send($param);
        if($data['code'] == 0){
            return view('web.user.member.pay_revise2')->with(['data'=>$param]);
        }else{
            return redirect('/member/user/pay_revise?message='.$data['message']);
        }

    }

    public function payReviseThAction(){

        $this->validate($this->request, [
            'paypwd' => 'required',
        ],[
            'paypwd' => '支付密码不能为空'
        ]);

        $params = [
            'pay_pwd' => $this->request->input('paypwd'),
            'uptype' => 'uppay'
        ];
        $data = http_web($this->api_url['update'],[session('user_id')])->send($params);

        if($data['code'] == 0){
            return view('web.user.member.pay_revise3');
        }else{
            return redirect('/member/user/pay_revise');
        }



    }

    /**
     * 店铺认证页面
     * @return
     */
    public function shop(){


        $ret = http_web($this->api_url['shop'])->send();

        $data=$ret['data']['data'];
        $arr=[
            'identification_type'=>$data['identification_type'],
            'company'=>$data['company'],
            'company_material'=>$data['company_material'],
        ];
        return view('web.user.seller.shop',$arr);
    }

    /**
     * 店铺开通
     * @return
     */
    public function shop_auth(){
        $ret = http_web($this->api_url['shop_auth'])->send();

        if($ret['data']){
            session('shop_id',$ret['data']['sp_id']);
        }
        return $ret;

    }

    public function admittance(){
        return view('web.user.seller.seller_admittance');

    }



}